<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="关键词" prop="searchtxt">
        <el-input
          v-model="queryParams.searchtxt"
          placeholder="请输入关键词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:activity:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:activity:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:activity:remove']"
        >删除</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="所属分类" align="center" prop="referrerTypeName" />
      <el-table-column label="律所名称" align="center" prop="lawfirmName" />
      <el-table-column label="合伙人数" align="center" prop="partnerNum" />
      <el-table-column label="被推荐的律师数" align="center" prop="referrerNum" />
      <el-table-column label="被推荐律师占比合伙人%" align="center" prop="referrerPercentage" />
      <el-table-column label="平均占比%" align="center" prop="averagePercentage" />
      <el-table-column label="排序" align="sort" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            class="text-red"
            icon="el-icon-remove-outline"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!-- <el-form-item label="所属分类" prop="referrerType">
          <el-input v-model="form.referrerType" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item label="关联律所" prop="lawfirmId">
          <el-select v-model="form.lawfirmId" filterable placeholder="请选择">
            <el-option v-for="item in lawfirmList" :key="item.id" :label="item.lawfirmFullname" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="律所名称" prop="lawfirmName">
          <el-input v-model="form.lawfirmName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="合伙人数" prop="partnerNum">
          <el-input v-model="form.partnerNum" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="被推荐的律师数" prop="referrerNum">
          <el-input v-model="form.referrerNum" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="被推荐的律师占比合伙人%" prop="referrerPercentage">
          <el-input v-model="form.referrerPercentage" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="被推荐律师地域分布" prop="referrerCity">
          <el-input v-model="form.referrerCity" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="被推荐的专业领域个数" prop="specialityNum">
          <el-input v-model="form.specialityNum" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="参加推荐的客户数" prop="customerNum">
          <el-input v-model="form.customerNum" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="推荐客户占比合伙人%" prop="customerPercentage">
          <el-input v-model="form.customerPercentage" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="参与推荐的客户是500强企业的" prop="topNum">
          <el-input v-model="form.topNum" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="500强客户占比合伙人%" prop="topPercentage">
          <el-input v-model="form.topPercentage" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="500强企业客户占推荐客户比例%" prop="referrerTopPercentage">
          <el-input v-model="form.referrerTopPercentage" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="推荐总数" prop="referrerCount">
          <el-input v-model="form.referrerCount" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="推荐总数占比合伙人%" prop="percentage">
          <el-input v-model="form.percentage" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="推荐总数/被推荐的律师数" prop="referrer">
          <el-input v-model="form.referrer" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="平均占比%" prop="averagePercentage">
          <el-input v-model="form.averagePercentage" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="推荐理由" prop="reason">
          <el-input v-model="form.reason" type="textarea" :rows="3" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="1"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryList, getItem, delItem, addItem, updateItem } from "@/api/official/referrer2024/lawfirm"
import { getReferrerLawfirmList } from "@/api/official/referrer"

export default {
  name: "User",
  dicts: ['job_level', 'business_citys', 'enterprises_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchtxt: null,
      },
      lawfirmList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      imgAction: `${process.env.VUE_APP_BASE_API}/common/ftpCommonUpload`,
    }
  },
  created() {
    getReferrerLawfirmList().then(response => {
      this.lawfirmList = response.data
    })
    this.getList()
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      queryList(this.queryParams).then(response => {
        this.userList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      // const id = row.id || this.ids
      // getItem(id).then(response => {
        this.form = {...row}
        this.open = true
        this.title = `修改信息`
      // })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除【信息编号为"' + ids + '"的数据项？').then(function() {
        return delItem(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    // 状态修改
    handleStatusChange(row, field='status') {
      let text = row[field] == 0 ? "停用" : "启用"
      this.$modal.confirm(`确认要"${text}"该企业吗？`).then(function() {
        return updateItem({id :row.id, [field]: row[field]})
      }).then(() => {
        this.$modal.msgSuccess(text + "成功")
      }).catch(function() {
        row[field] = row[field] == "0" ? 1 : 0
      })
    },
    beforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 5
      if (!isRightSize) {
        this.$message.error('文件大小超过 5MB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
    uploadSuccess(res){
      this.form.officeImages = res.path;
    }
  }
}
</script>

<style scoped>
.more-radio-group .el-radio{
  margin-top: 10px;
}
.other-input {
  margin-top: 8px;
}
</style>
